package com.hzjq.encrypt.ui

import android.content.Context
import android.content.Intent
import com.hzjq.encrypt.adapter.JbqyAdapter
import com.hzjq.encrypt.bean.EnBean
import com.hzjq.encrypt.bean.Jbqy
import com.hzjq.encrypt.bean.Jbqys
import com.hzjq.encrypt.pop.JbqyEditorPop

class JbqyActivity : AbsListActivity<Jbqy>() {

    companion object {
        fun start(context: Context, data: EnBean?) {
            val intent = Intent(context, JbqyActivity::class.java)
            if (data == null) {
                intent.putExtra("data", EnBean())
            } else {
                intent.putExtra("data", data)
            }
            context.startActivity(intent)
        }
    }


    private val mJbqyAdapter = JbqyAdapter(arrayListOf())

    override fun onDataCreated() {
        mAdapter = this.mJbqyAdapter

        if (data.jbqys == null) {
            data.jbqys = Jbqys()
        }
        if (data.jbqys.jbqy.isNullOrEmpty()) {
            data.jbqys.jbqy = arrayListOf()
        }

        mJbqyAdapter.setNewInstance(data.jbqys.jbqy)

        setShowTitle("禁爆区域(${data.jbqys.jbqy.size})个")
    }

    override fun onRemoveItemClick(position: Int) {
        data.jbqys.jbqy.removeAt(position)
        setShowTitle("禁爆区域(${data.jbqys.jbqy.size})个")
        mJbqyAdapter.setNewInstance(data.jbqys.jbqy)
        notifyDataChanged()
    }

    override fun onItemEditorClick(position: Int) {
        JbqyEditorPop.show(this,object : JbqyEditorPop.OnJbqyEditorCallback{
            override fun onLgEditor(jbqy: Jbqy) {
                data.jbqys.jbqy[jbqy.position] = jbqy
                mJbqyAdapter.notifyDataSetChanged()
                notifyDataChanged()
            }
        },mJbqyAdapter.getItem(position))
    }

    override fun onAddNewClick() {
        val lge = Jbqy()
        lge.position = -1
        JbqyEditorPop.show(this,object : JbqyEditorPop.OnJbqyEditorCallback{
            override fun onLgEditor(jbqy: Jbqy) {
                data.jbqys.jbqy.add(0, jbqy)
                setShowTitle("禁爆区域(${data.jbqys.jbqy.size})个")
                mJbqyAdapter.setNewInstance(data.jbqys.jbqy)
                notifyDataChanged()
            }
        },lge)
    }
}